'use strict';

module.exports = app => {
    return class StudyService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }


        /**
         * 今日作业
         * @param id
         * @returns {Promise<void>}
         */
        async todayHomework({ studentId }) {
            let url = this.app.urls('todayHomework');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    studentId
                }
            });
			// this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 历史作业
         * @param studentId
         * @returns {Promise<void>}
         */
        async history({ studentId }) {
            let url = this.app.urls('historyHomework');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    studentId
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        /**
         * 获取一道题
         * @param id
         * @param studentId
         * @returns {Promise<void>}
         */
        async one({ id, studentId }) {
            let url = this.app.urls('getQuestion');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    studentTaskId: id,
                    studentId
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        async submit(params) {
            let url = this.app.urls('homeworkSubmit');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data: {
                    studentTaskId: params.id,
                    choiceStringJson: params.choiceStringJson
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 未完成的作业
         * @param studentId
         * @returns {Promise<void>}
         */
        async unfinished({ studentId }) {
            let url = this.app.urls('unfinishedTask');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    studentId
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 错题详解
         * @param id
         * @param studentId
         * @returns {Promise<void>}
         */
        async errorDetail({ id, studentId }) {
            let url = this.app.urls('errorDetail');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    studentTaskId: id,
                    studentId
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        /**
         * 题目详解
         * @param id
         * @param studentId
         * @returns {Promise<void>}
         */
        async questionDetail({ id, studentId }) {
            let url = this.app.urls('questionDetaile');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    studentTaskId: id,
                    studentId
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 单个题目详解
         * @param id
         * @param studentId
         * @returns {Promise<void>}
         */
        async detail({ id, studentId }) {
            let url = this.app.urls('taskQuestionDetaile');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    studentTaskId: id,
                    studentId
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }



        /**
         * 作业报告
         * @param id
         * @param studentId
         * @param type
         * @param role
         * @returns {Promise<void>}
         */
        async report({ id }) {
            let url = this.app.urls('studentTaskReport');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

    }

};
